package org.lds.scheduled.job.worker.scheduler.service;

import lombok.RequiredArgsConstructor;
import org.lds.scheduled.job.common.model.AddJobModel;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RequiredArgsConstructor
@Service
public class AutoSchedulerServiceImpl implements AutoSchedulerService {
    private final SchedulerService schedulerService;

    /**
     * key: jobId
     * value: jobModel
     */
    private final Map<Long, AddJobModel> prevJobModelMap = new ConcurrentHashMap<>();

    /**
     * key: jobId
     * value: jobModel
     */
    private final Map<Long, AddJobModel> currJobModelMap = new ConcurrentHashMap<>();

    public synchronized void resetCurrJobModelMap(Map<Long, AddJobModel> currJobModelMap) {
        this.currJobModelMap.clear();
        this.currJobModelMap.putAll(currJobModelMap);

        updatePrevJobModelMap();
    }

    public synchronized void updatePrevJobModelMap() {
        prevJobModelMap.forEach((jobId, jobModel) -> {
            if (currJobModelMap.containsKey(jobId)) {
                // 更新
                //schedulerService.updateJob();
            } else {
                // 删除
            }
        });

        currJobModelMap.forEach((jobId, jobModel) -> {
            if (!prevJobModelMap.containsKey(jobId)) {
                // 新增
            }
        });
    }

}
